package galkin.id.project;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ScenarioParser {
	Document dom;
	ArrayList<Scenario> myScenarios = new ArrayList<Scenario>();

	public Document getDom() {
		return dom;
	}

	public void setDom(Document dom) {
		this.dom = dom;
	}

	public ArrayList<Scenario> getMyScenarios() {
		return myScenarios;
	}

	public void setMyScenarios(ArrayList<Scenario> myScenarios) {
		this.myScenarios = myScenarios;
	}

	public void parseXmlFile(String file) {
		// get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {

			// Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			// parse using builder to get DOM representation of the XML file
			dom = db.parse(ScenarioParser.class.getResourceAsStream(file));
			//dom = db.parse(file);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (SAXException se) {
			se.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public void initilizeScenarios() {
		Element docEle = dom.getDocumentElement();

		NodeList nl = docEle.getElementsByTagName("scenario");
		if (nl != null && nl.getLength() > 0) {
			for (int i = 0; i < nl.getLength(); i++) {
Scenario scenario=new Scenario();
				// get the employee element
				Element scenarioEl = (Element) nl.item(i);

				String id = scenarioEl.getAttribute("id");
			//	System.out.println(id);
				scenario.setId(Integer.parseInt( id ));
				// get the Employee object
				Node storynode = scenarioEl.getElementsByTagName("story").item(
						0);
				String story = storynode.getTextContent().trim();
			//	System.out.println(story);
				scenario.setStory(story);
				NodeList optionnodes = scenarioEl
						.getElementsByTagName("option");
				ArrayList <ScenarioOption> scenariooption=new ArrayList <ScenarioOption>();
				for (int o = 0; o < optionnodes.getLength(); o++) {
					ScenarioOption option=new ScenarioOption();
					Element optionEl = (Element) optionnodes.item(o);
				//	System.out.println(optionEl.getTextContent());
					option.setChoice(optionEl.getTextContent().trim());
					String s = optionEl.getAttribute("scenarioId");
				//	System.out.println(s);
					option.setScenarioId( Integer.parseInt( s ));

				//	System.out.println (optionEl.getTextContent());
					scenariooption.add(option);
				}
				scenario.setOptions(scenariooption);
				
				myScenarios.add(scenario);
			}
		}
	}

	public Scenario getScenariobyId(int id) {
		Scenario n=null;
		for (int p=0; p<myScenarios.size(); p++) {
		if (myScenarios.get(p).getId()==id) {
			n= myScenarios.get(p);
			break; 
		}
		}
		return n;
		
		
		
		

		// String option=optionnodes.getTextContent();
		// System.out.println (option);

	}

}